index_app.controller("inventory_settings_edit", function ($scope,$http,$routeParams,$timeout) {
	$scope.params=$routeParams;
	$scope.detailedKey = $scope.params.key;
    $scope.isCanEdit = $scope.params.isEdit;

    if($scope.isCanEdit == "outGoing"){
        $scope.outGoing = true;
        $scope.storage = false;
    } else if($scope.isCanEdit == "storage"){
          $scope.outGoing = false;
          $scope.storage = true;
    }


	$http.post("commodityDetailed/selectByKey?detailedKey="+$scope.detailedKey).then(function(response){
		//数据源
		$scope.inverntoryDetails = response.data;
		$("#quantity").val("");
		$("#outquantity").val("");
		$("#remarks").val("");
	});

	$scope.returnList = function(){
		$("#returnButton").attr("href","#inventory_settings_list");
	};


	//用户信息修改页面中保存操作
	$scope.inventoryUpdate = function(inverntoryDetails){
	    console.log(inverntoryDetails.detailedKey);
	    $scope.typeKey = inverntoryDetails.skuType.typeKey;
        if($scope.isCanEdit == "outGoing"){
            if(inverntoryDetails.quantity > inverntoryDetails.stock ){
                $scope.title = "保存失败";
                $scope.toolTip = "出库数量大于当前库存数，请重新填写出库数量";
        	    toolTipDialog();
        	    $scope.updateFlag = false;
        	} else {
        	    inverntoryDetails.stock = (inverntoryDetails.stock*1) - (inverntoryDetails.quantity*1);
        	     $scope.updateFlag = true;
        	}
        	inverntoryDetails.type = "出库";
        } else if($scope.isCanEdit == "storage"){
            inverntoryDetails.stock = (inverntoryDetails.stock*1) + (inverntoryDetails.quantity*1);
            inverntoryDetails.type = "入库";
            $scope.updateFlag = true;
        }
        if( $scope.updateFlag ){
         inverntoryDetails.skuType = null;
        		$http({
        			method  : 'POST',
        			url     : 'inventoryDetails/saveInventoryDetails?typekey='+$scope.typeKey+"&detailsKey="+inverntoryDetails.detailedKey,
        			params    : inverntoryDetails,//传递真个对象到后台
        			headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
        		}).success(function(data) {
        			if(data == 1){
        				//添加保存标题样式
        				$scope.title = '<h4 class="easyDialog_title" id="easyDialogTitle"><i class="icon-ok green"></i> <a href="javascript:void(0)" title="关闭窗口" class="close_btn" id="closeBtn">&times;</a>保存</h4>';
        				//保存成功内容
        				$scope.toolTip = "保存成功";
        				//弹窗
        				toolTipDialog();
        				$scope.success = true;

        			} else {
        				$scope.title = "保存";
        				$scope.toolTip = "保存失败";
        				toolTipDialog();
        			}
        		});
        }

	};

	function toolTipDialog(){
    		easyDialog.open({
    			container : {
    				header : $scope.title,
    				content : $scope.toolTip,
    				yesFn : $scope.toolFn,
    				noFn : false
    			},
    			fixed : false
    		});
    	}
    	//弹窗中确定按钮事件
        	$scope.toolFn = function(){
        		if( $scope.success){//成功
        			window.location.href="#inventory_settings_list";
        		}
        		return true;
        	}

})